package com.per.mybootall.service.impl;

import com.google.common.collect.Lists;
import com.per.mybootall.dao.*;
import com.per.mybootall.pojo.Order;
import com.per.mybootall.pojo.Product;
import com.per.mybootall.pojo.RoleAndUser;
import com.per.mybootall.pojo.User;
import com.per.mybootall.service.AdminDoInter;
import com.per.mybootall.vo.AdminLoadVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminDoimpl implements AdminDoInter {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleAndUserMapper roleAndUserMapper;
    @Autowired
    private  Product product;

    public static   String msg="";
    //根据传递的条件进行页面的加载
    @Override
    public Map<Object,Object> adminLoad(String userToken, String whoareRoot, String productKind){

        System.out.println("userToken:"+userToken);
        System.out.println("whoareRoot:"+whoareRoot);

        String userTokens=userToken.substring(1,userToken.length()-1);
        String who=whoareRoot.substring(1,whoareRoot.length()-1);


        Map<Object,Object> map=new HashMap<Object, Object>();

        String tokeninredis=(String)redisTemplate.opsForValue().get(who);
        if(!userTokens.equals(tokeninredis)){
            System.out.println("管理员身份验证不通过");
            map.put("msg","身份验证不通过");
            return map;
        }
        //管理员身份验证通过=======>>>>>>>>条件页面的加载
        List<AdminLoadVo> list=productMapper.selectProductAndCategoryMessage(productKind);
        System.out.println("管理员展示的数据：");
        System.out.println(list.toString());
        System.out.println("========>>>>>>>"+list.size());
        map.put("list",list);
        return map;
    }

    /*删除product信息*/
    @Override
    public String delectProductMessgae(Integer productid){
       int result= productMapper.delectProductMessage(productid);
        System.out.println("删除结果"+result);
        if(result>=1){
            return   msg="success";
        }
        return  msg="error";
    }


    @Override
    public String delectOrderMessage(Integer id){

        int result=orderMapper.deleteByPrimaryKey(id);
        if(result==0){
            return   msg="error";
        }
        return msg="success";
    }


    @Override
    public Map<String,Object> orderLoadMessage(String roottoken, String whoareroot){

        System.out.println("参数:"+roottoken+","+whoareroot);

       Map<String,Object> map=new HashMap<String,Object>();
//        String roottokens=roottoken.substring(1,roottoken.length()-1);
//        String who=whoareroot.substring(1,whoareroot.length()-1);
        String whoInRedis=(String)redisTemplate.opsForValue().get(whoareroot);
        if(roottoken.equals(whoInRedis)){
            map.put("msg","身份验证不通过");
            return map;
        }
        /*for()*/
        List<Order> orderList= Lists.newArrayList();
        for (int i=1;i<11;i++){
            Order order=new Order();
            order=orderMapper.selectByPrimaryKey(i);
            orderList.add(order);
        }
        System.out.println("all  order  message:"+orderList);
        map.put("list",orderList);
        return map;
    }

    @Override
    @Transactional
    public Map<String,Object> loadUserMessage(String roottoken, String whoareroot){

        System.out.println("参数:"+roottoken+","+whoareroot);

        Map<String,Object> map=new HashMap<String,Object>();

        String whoInRedis=(String)redisTemplate.opsForValue().get(whoareroot);
        if(roottoken.equals(whoInRedis)){
            map.put("msg","身份验证不通过");
            return map;
        }
        /*for()*/
        List<User> userList= Lists.newArrayList();
        List<String> roleForWhoName=  roleAndUserMapper.selectAllRole();
        System.out.println("++++++>>>>>>"+roleForWhoName);
        for(int i=0;i<roleForWhoName.size();i++){

        User user= userMapper.selectAllForUsername(roleForWhoName.get(i));
        userList.add(user);
        }

        System.out.println("all  order  message:"+userList);
        map.put("list",userList);
        return map;
    }

/*删除user信息*/
    @Override
    public String delectUserMessage(Integer id){
        int result=  userMapper.deleteByPrimaryKey(id);
      if(result>0){
          return   msg="success";

      }
        return  msg="error";
    }



    @Override
    public String updateUserMessage(User record){
      int result=  userMapper.updateByPrimaryKeySelective(record);
      if(result==0){
          return msg="error";

      }
        return msg="success";
    }

    @Override
    public String updateProductMessage(String subtitle, BigDecimal prick, Integer stock, Long id){
        if(subtitle==null||"".equals(subtitle)||prick==null||stock==null||id==null){
            System.out.println("参数不可为null或者空格");
           return msg="参数不可为null或者空格";
        }
        product.setSubtitle(subtitle);    product.setPrick(prick);
        product.setStock(stock);  product.setId(id);

        int re=productMapper.updateByPrimaryKeySelective(product);
        System.out.println(re);

        if(re==0){
            System.out.println("scscscsdcs");
            return msg="error";
        }
        System.out.println("scscscsdcs");
      return msg="success";
    }


    @Override
    public  String updateOrderMessage(Order order){
        if(orderMapper.updateByPrimaryKeySelective(order)==0){
            return msg="error";
        }
        return msg="success";
    }

}
